﻿using IntoCommon.Util;
using IntoMes.Global;
using IntoMes.Models;
using IntoMes.PLC;
using IntoMes.Views;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Input;

namespace IntoMes.ViewModels
{
    public class SparePartsManageViewModel : BindableBase
    {
        private int mesStatus = 0;
        private int productMode = 0;
        private int productNum = 500;
        private int personDayShift = 0;

        private int hasProductNum = 0;
        private int badProductNum = 0;
        private string goodPercent = "0%";
        public bool IsChangeModel = false;

        #region 型号列表 

        private ObservableCollection<string> _Recipes;
        /// <summary>
        /// 型号列表
        /// </summary>
        public ObservableCollection<string> Recipes
        {
            get { return _Recipes; }
            set
            {
                _Recipes = value;
                RaisePropertyChanged("Recipes");
            }
        }
        #endregion 型号列表


        public int HasProductNum
        {
            get => hasProductNum; set => SetProperty(ref hasProductNum, value);
        }
        public int BadProductNum
        {
            get => badProductNum; set => SetProperty(ref badProductNum, value);
        }

        public string GoodPercent
        {
            get => goodPercent; set => SetProperty(ref goodPercent, value);
        }
        public int MesStatus
        {
            get => mesStatus; set => SetProperty(ref mesStatus, value);
        }
        public int ProductMode
        {
            get => productMode; set => SetProperty(ref productMode, value);
        }
        public int PersonDayShift
        {
            get => personDayShift; set => SetProperty(ref personDayShift, value);
        }
        public int ProductNum
        {
            get => productNum; set => SetProperty(ref productNum, value);
        }

        public SparePartsManageViewModel()
        {
            RefreshModels();
        }
        public void RefreshModels()
        {
            Recipes = new ObservableCollection<string>();
            if (File.Exists(Paths.recipeCfgFile))
            {
                var recipes = GlobalFunc.LoadXmlFile<List<string>>(Paths.recipeCfgFile);
                foreach (var recipe in recipes)
                {
                    Recipes.Add(recipe);
                }
            }
            else
            {
                Recipes.Add("HK");
                GlobalFunc.WriteXmlFile(Paths.recipeCfgFile, Recipes);
            }
        }

        private ProductionQueryViewModel prodcutionQuery => Locator.Instance.ProdcutionQuery;
        private void Send()
        {
            MesSetData setData = new MesSetData();
            setData.ProModel = (byte)(productMode + 1);
            setData.ProCount = productNum;
            setData.Class = (byte)(personDayShift + 1);
            bool isMes = mesStatus == 0;
            bool isClear =false;
            var main = Locator.Instance.StationStatus;
            if (!string.IsNullOrEmpty(main.CurModel) && main.CurModel != Recipes[productMode])
            {
                IsChangeModel = true;

            }
            if (IsChangeModel || prodcutionQuery.GetProductPlanCount(Recipes[productMode])==0)
            {
                isClear = true;
            }
            Locator.Instance.Service.Set(setData, isMes,isClear);
            main.CurModel = Recipes[productMode];
            main.CurBc = personDayShift+1 == 1 ? "白班" : "夜班";
            ProductNum = productNum;
            IssProducePlan p = new IssProducePlan();
            p.CurBc = main.CurBc;
            p.ProductMode = main.CurModel;
            p.PlanCount = ProductNum;
            p.DateTime = System.DateTime.Today;
            prodcutionQuery.LogProductPlan(p);
            prodcutionQuery.GetProductNum(true, main.CurModel);
            //Locator.Instance.ProductionStatus.SparePartsManage.ProductNum = productNum;
            HandyControl.Controls.MessageBox.Show("下发成功");

            //直接写PLC的配方等数据，各个工位读取该数据显示即可
           



        }

        private DelegateCommand sendCmd;
        public DelegateCommand SendCmd => sendCmd ?? (sendCmd = new DelegateCommand(Send));

        #region 新增型号 命令

        /// <summary>
        /// 新增型号命令
        /// </summary>
        public ICommand AddModelCommand => new DelegateCommand(() =>
        {
            AddProductModelWidow win = new AddProductModelWidow();
            win.ShowDialog();
        });

        #endregion 新增型号

    }
}
